package com.wman.sheep.mvp.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.File;

/**
 * Created by Administrator on 2017/4/5.
 * 视图层代理基类
 */

public abstract class AppDelegate implements IDelegate {

    protected final SparseArray<View> mViews = new SparseArray<>();

    protected View mRootView;

    protected abstract int getRootLayoutId();

    @Override
    public void create(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int rootLayoutId = getRootLayoutId();
        if (rootLayoutId > 0) {
            mRootView = inflater.inflate(rootLayoutId, container, false);
        }
    }

    @Override
    public int getOptionsMenuId() {
        return 0;
    }

    @Override
    public Toolbar getToolBar() {
        return null;
    }

    @Override
    public View getRootView() {
        return mRootView;
    }

    public void setRootView(View rootView) {
        mRootView = rootView;
    }

    @Override
    public void initWidget() {

    }

    public <T extends View> T bindView(int id) {
        T view = (T) mViews.get(id);
        if (view == null) {
            view = (T) mRootView.findViewById(id);
            mViews.put(id, view);
        }

        return view;
    }

    public <T extends View> T get(int id) {
        return bindView(id);
    }

    public void setOnClickListener(View.OnClickListener listener, int... ids) {
        if (ids == null) {
            return;
        }
        for (int id : ids) {
            get(id).setOnClickListener(listener);
        }
    }

    public <T extends Activity> T getActivity() {
        return (T) mRootView.getContext();
    }

    public void loadImage(Context context, ImageView imageView, String url) {
        Glide.with(context)
                .load(url)
                .into(imageView);
    }
    public void loadImage(Context context, ImageView imageView, String url,int rid) {
        Glide.with(context)
                .load(url)
                .dontAnimate()
                .placeholder(rid)
                .into(imageView);

    }
    public void loadImage(Context context, ImageView imageView, File file) {
        Glide.with(context)
                .load(file)
                .centerCrop()
                .into(imageView);
    }

}
